package com.kehutong.api.action;


import com.kehutong.common.Authutil.OAuth2Provider;
import com.kehutong.common.DoveClient;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.oauth2.OAuth2Accessor;
import org.coraframework.oauth2.OAuth2ProblemException;
import org.coraframework.util.Objects;

/**
 * 小区管理
 * @author: liul
 * @date: 2021-06-11 09:50
 */
@MvcAction
@ReqMapping("/oau-kht/community")
public class CommunityAction {
    private static final Logger logger = LoggerFactory.getLogger(CrganizationAction.class);
    @Inject
    private OAuth2Provider oauth2Provider;

    @Inject
    private DoveClient doveClient;
    @ReqMapping("/save")
    public Object create(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.remove("token");
        JSONObject residentHouseResult = doveClient.post("/basic/community/service/api/save", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (residentHouseResult.getIntValue("code") == 200) {
            return residentHouseResult.getJSONObject("data");
        }
        return residentHouseResult;
    }

    @ReqMapping("/update")
    public Object update(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.remove("token");
        JSONObject residentHouseResult = doveClient.post("/basic/community/service/api/update", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (residentHouseResult.getIntValue("code") == 200) {
            return residentHouseResult.getJSONObject("data");
        }
        return residentHouseResult;
    }

    @ReqMapping("/delete")
    public Object delete(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.remove("token");
        JSONObject residentHouseResult = doveClient.post("/basic/community/service/api/delete", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (residentHouseResult.getIntValue("code") == 200) {
            return Result.success();
        }
        return residentHouseResult;
    }

    @ReqMapping("/list")
    public Object buildingList(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.remove("token");
        JSONObject residentHouseResult = doveClient.post("/basic/community/service/api/pageList", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (residentHouseResult.getIntValue("code") == 200) {
            return residentHouseResult.getJSONObject("data");
        }
        return residentHouseResult;
    }

}
